package com.ss.android.article.browser.main;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.CoordinatorLayout.Behavior;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;
import com.ss.android.common.ui.view.ScrollControllableViewPager;
import com.ss.android.common.util.ImmersedStatusBarHelper;

import java.lang.ref.WeakReference;

public class BrowserBehavior extends Behavior {
    public interface a {
        boolean a();
    }

    public interface BrowserBehavior$b {
        void a(float arg1);

        void b(float arg1);
    }

    private static final String TAG = "BrowserBehavior";
    boolean isScrolling;
    private final float mCategoryBottomThreshold;
    private View mCategoryGuideLayout;
    private View mCategoryGuideLayoutBg;
    private View mCategoryLayout;
    private View mCategoryLayoutPlaceHolder;
    private final float mCategoryTopThreshold;
    private final float mSearchBarRealHeightScoleMultiple;
    private float mSearchBarRealWidthScoleMultiple;
    private View mSearchLayout;
    private View mSearchLayoutContainer;
    private View mSearchViewBackgroundExternal;
    private View mSearchViewBackgroundInternal;
    private final int mStatusBarHeight;
    private int mTopBarHeight;
    private View mTopHeaderBox;
    private int mTotalScrollHeight;
    private ScrollControllableViewPager mViewPager;
    private View mWebSiteGuideLayout;
    private View mWhetherLayout;
    private static WeakReference sCallbackRef;
    private static WeakReference sListenerRef;

    public BrowserBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.isScrolling = false;
        this.mTopBarHeight = context.getResources().getDimensionPixelOffset(R.dimen.gq);
        this.mStatusBarHeight = ImmersedStatusBarHelper.getStatusBarHeight(context, true);
        this.mCategoryBottomThreshold = UIUtils.dp2px(context, 10f);
        this.mCategoryTopThreshold = this.mCategoryBottomThreshold * 3f;
        this.mSearchBarRealWidthScoleMultiple = ((((float) UIUtils.getScreenWidth(context))) - UIUtils.dp2px(context, 12f) * 2f) / ((((float) UIUtils.getScreenWidth(context))) - UIUtils.dp2px(context, 15f) * 2f);
        this.mSearchBarRealHeightScoleMultiple = (((float) context.getResources().getDimensionPixelOffset(R.dimen.eu))) / (((float) context.getResources().getDimensionPixelOffset(R.dimen.c6)));
    }

    private boolean animationCategoryBar(float translationY) {
        float v2 = 1f;
        float v0 = (translationY - this.mCategoryBottomThreshold) / (this.mCategoryTopThreshold - this.mCategoryBottomThreshold);
        if (v0 < 0f) {
            v0 = 0f;
        } else if (v0 > v2) {
            v0 = v2;
        }

        if (this.mCategoryLayoutPlaceHolder != null) {
            this.mCategoryLayoutPlaceHolder.setAlpha(v2 - v0);
        }

        if (v0 == v2) {
            this.mViewPager.setScroll(true);
        } else if (v0 == 0f) {
            this.mViewPager.setScroll(false);
        }

        return true;
    }

    private boolean animationCategoryGuideLayout(float translationY) {
        boolean v1 = false;
        float v3 = 1f;
        if (this.mCategoryGuideLayout != null) {
            int v4 = this.mCategoryGuideLayout.getMeasuredHeight();
            int v0 = this.mWebSiteGuideLayout == null ? 0 : this.mWebSiteGuideLayout.getMeasuredHeight();
            if (v0 / 2 + v4 == 0) {
                return v1;
            }

            float v0_1 = (translationY - (((float) (v0 / 2)))) / (((float) (v0 / 2 + v4)));
            if (v0_1 < 0f) {
                v0_1 = 0f;
            } else if (v0_1 > v3) {
                v0_1 = v3;
            }

            this.mCategoryGuideLayout.setAlpha(v3 - v0_1);
            this.mCategoryGuideLayout.setScaleX(v3 - v0_1 * 0.1f);
            v1 = true;
        }

        return v1;
    }

    private boolean animationSearchLayoutContainer(float translationY) {
        float v0 = 0f;
        float v1 = 1f;
        if (this.mSearchLayoutContainer != null) {
            int v2 = this.mSearchLayoutContainer.getMeasuredHeight() - this.mStatusBarHeight - this.mSearchLayout.getMeasuredHeight();
            int v3 = this.mTotalScrollHeight - v2;
            if (v2 != 0) {
                float v2_1 = (translationY - (((float) v3))) / (((float) v2));
                if (v2_1 >= 0f) {
                    v0 = v2_1 > v1 ? v1 : v2_1;
                }

                this.mWhetherLayout.setAlpha(v1 - v0);
                this.mCategoryGuideLayoutBg.setAlpha(v1 - v0);
                this.mWhetherLayout.setScaleX(v1 - 0.1f * v0);
                this.mSearchViewBackgroundExternal.setAlpha(v0);
                this.mSearchViewBackgroundInternal.setScaleX((this.mSearchBarRealWidthScoleMultiple - v1) * v0 + v1);
                this.mSearchViewBackgroundInternal.setScaleY(v1 + (this.mSearchBarRealHeightScoleMultiple - v1) * v0 / 4f);
                this.mSearchLayout.setTranslationY(v0 * (((float) (-(this.mWhetherLayout.getMeasuredHeight() - this.mStatusBarHeight)))));
            }
        }

        return false;
    }

    private boolean animationWebSiteGuideLayout(float translationY) {
        boolean v0_1;
        float v0 = 0f;
        float v1 = 1f;
        if (this.mWebSiteGuideLayout == null) {
            v0_1 = false;
        } else {
            int v3 = this.mWebSiteGuideLayout.getMeasuredHeight();
            if (v3 == 0) {
                v0_1 = false;
            } else {
                float v2 = translationY / (((float) v3));
                if (v2 >= 0f) {
                    v0 = v2 > v1 ? v1 : v2;
                }

                this.mWebSiteGuideLayout.setAlpha(v1 - v0);
                this.mWebSiteGuideLayout.setScaleX(v1 - v0 * 0.1f);
                v0_1 = true;
            }
        }

        return v0_1;
    }

    public boolean layoutDependsOn(CoordinatorLayout coordinatorLayout, View child, View dependency) {
        return this.layoutDependsOn(coordinatorLayout, ((FrameLayout) child), dependency);
    }

    public boolean layoutDependsOn(CoordinatorLayout coordinatorLayout, FrameLayout frameLayout, View dependency) {
        boolean isTargetChildView = dependency != null && dependency.getId() == R.id.feed_area_container;
        if (isTargetChildView) {
            this.mWhetherLayout = frameLayout.findViewById(com.ss.android.article.browser.R.id.ll_weather_container);
            this.mSearchLayoutContainer = frameLayout.findViewById(com.ss.android.article.browser.R.id.home_page_header_layout);
            this.mSearchLayout = frameLayout.findViewById(com.ss.android.article.browser.R.id.home_page_fake_search_layout);
            this.mWebSiteGuideLayout = frameLayout.findViewById(com.ss.android.article.browser.R.id.website_navi_grid);
            this.mCategoryGuideLayout = frameLayout.findViewById(com.ss.android.article.browser.R.id.category_navi_grid);
            this.mCategoryGuideLayoutBg = frameLayout.findViewById(com.ss.android.article.browser.R.id.yy);
            this.mViewPager = dependency.findViewById(com.ss.android.article.browser.R.id.view_pager);
            this.mSearchViewBackgroundInternal = frameLayout.findViewById(com.ss.android.article.browser.R.id.home_page_fake_search_input_box_bg);
            this.mSearchViewBackgroundExternal = frameLayout.findViewById(com.ss.android.article.browser.R.id.yz);
            this.mTopHeaderBox = frameLayout.findViewById(com.ss.android.article.browser.R.id.browser_guid_scroll_top_layout);
        }
        return isTargetChildView;
    }

    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        return this.onDependentViewChanged(parent, ((FrameLayout) child), dependency);
    }

    public boolean onDependentViewChanged(CoordinatorLayout parent, FrameLayout child, View view) {
        float childTranslationY = Math.abs(view.getTranslationY());
        if (this.mTotalScrollHeight <= 0) {
            this.mTotalScrollHeight = child.getMeasuredHeight() - this.mStatusBarHeight - this.mSearchLayout.getMeasuredHeight();
        }

        boolean v0 = this.animationWebSiteGuideLayout(childTranslationY)
                | this.animationCategoryGuideLayout(childTranslationY)
                | this.animationSearchLayoutContainer(childTranslationY);
        if (this.mCategoryLayout == null) {
            this.mCategoryLayout = view.findViewById(com.ss.android.article.browser.R.id.category_layout);
        }

        if (this.mCategoryLayoutPlaceHolder == null) {
            this.mCategoryLayoutPlaceHolder = view.findViewById(com.ss.android.article.browser.R.id.zk);
        }

        boolean v4 = v0 | this.animationCategoryBar(childTranslationY);
        boolean v1 = childTranslationY <= 0f || childTranslationY >= (((float) this.mTotalScrollHeight)) ? false : true;
        Object v0_1;
        if (v1 != this.isScrolling) {
            this.isScrolling = v1;
            if (BrowserBehavior.sListenerRef != null) {
                v0_1 = BrowserBehavior.sListenerRef.get();
                if (v0_1 != null) {
                    if (v1) {
                        ((BrowserBehavior$b) v0_1).a(childTranslationY);
                    } else {
                        ((BrowserBehavior$b) v0_1).b(childTranslationY);
                        View v0_2 = this.mCategoryLayoutPlaceHolder;
                        boolean v2 = true;
                        if (childTranslationY > 0f) {
                            v2 = false;
                        }
                        v0_2.setClickable(v2);
                    }
                }
            }
        }

        if (BrowserBehavior.sCallbackRef != null) {
            v0_1 = BrowserBehavior.sCallbackRef.get();
            if (v0_1 != null) {
                v0 = ((a) v0_1).a() | v4;
            } else {
                v0 = v4;
            }
        } else {
            label_63:
            v0 = v4;
        }

        return v0;
    }

    public static void setOnScrollEventListener(BrowserBehavior$b arg1) {
        BrowserBehavior.sListenerRef = new WeakReference(arg1);
    }

    public static void setViewChangeCallback(a arg1) {
        BrowserBehavior.sCallbackRef = new WeakReference(arg1);
    }
}

